package com.tianyiyan.tyenword;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.Action;
import com.tianyiyan.dao.tyenword.BookDao;
import com.tianyiyan.dao.tyenword.VocabularyDao;
import com.tianyiyan.hibernate.tyenword.Book;
import com.tianyiyan.hibernate.tyenword.Vocabulary;
public class QuestionsAction implements Action, SessionAware
{
	private List<Elem> vocabularyList = new ArrayList<Elem>();
	
	private Map<String, Object> session;
	
	private Book book;
	
	private Integer bookId;
	
	private Integer unit;
	
	public Book getBook() {
		return book;
	}

	public void setBook(Book book) {
		this.book = book;
	}
	
	public List<Elem> getVocabularyList() {
		return vocabularyList;
	}

	public void setVocabularyList(List<Elem> vocabularyList) {
		this.vocabularyList = vocabularyList;
	}

	public Integer getBookId() {
		return bookId;
	}

	public void setBookId(Integer bookId) {
		this.bookId = bookId;
	}

	public Integer getUnit() {
		return unit;
	}

	public void setUnit(Integer unit) {
		this.unit = unit;
	}

	@Override
	public String execute() throws Exception {
		// System.out.println("QuestionsAction execute!");
		
		if (bookId == null || unit == null)
			return ERROR;
		
		if (session.get("username") == null)
			return LOGIN;
		
		BookDao bDao = new BookDao();
		book = bDao.getBook(bookId);
		
		VocabularyDao dao = new VocabularyDao();
		// System.out.println("Book : " + book);
		vocabularyList = dao.getMeaningList(book, unit);
		return SUCCESS;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.session = session;
	}

}
